package com.zhengqing.softdemo.controller

import com.google.gson.Gson
import com.zhengqing.softdemo.entity.Soft
import com.zhengqing.softdemo.service.SoftService
import com.mybatisflex.core.paginate.Page
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.io.Serializable

import com.zhengqing.softdemo.config.Slf4j;
import com.zhengqing.softdemo.config.Slf4j.Companion.log;

/**
 * 控制层。
 *
 * @author zql
 * @since 2024-04-25
 */
@Slf4j
@RestController
@RequestMapping("/soft")
class SoftController {
    @Autowired
    private val softService: SoftService? = null

    /**
     * 添加。
     *
     * @param soft
     * @return `true` 添加成功，`false` 添加失败
     */
    @PostMapping("save")
    fun save(@RequestBody soft: Soft): Boolean {
        return softService!!.save(soft)
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return `true` 删除成功，`false` 删除失败
     */
    @DeleteMapping("remove/{id}")
    fun remove(@PathVariable id: Serializable?): Boolean {
        return softService!!.removeById(id)
    }

    /**
     * 根据主键更新。
     *
     * @param soft
     * @return `true` 更新成功，`false` 更新失败
     */
    @PutMapping("update")
    fun update(@RequestBody soft: Soft): Boolean {
        return softService!!.updateById(soft)
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     * @GetMapping("list")
     *     fun list(): List<Soft> {
     *         return softService!!.list()
     *     }
     *
     *     List<Soft?>?
     */
    @GetMapping("list")
    fun list(): String? {
        val result = softService!!.list()
        return Gson().toJson(result)
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     * @GetMapping("getInfo/{id}")
     *     fun getInfo(@PathVariable id: Serializable?): Soft {
     *         return softService!!.getById(id)
     *     }
     */
    @GetMapping("getInfo/{id}")
    fun getInfo(@PathVariable id: Serializable?): String? {
        val result = softService!!.getById(id)
        return Gson().toJson(result)
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     * @GetMapping("page")
     *     fun page(page: Page<Soft?>): Page<Soft> {
     *         return softService!!.page(page)
     *     }
     */
    @GetMapping("page")
    fun page(page: Page<Soft?>): String? {
        val result = softService!!.page(page)
        return Gson().toJson(result)
    }

    // : Page<Soft?>?
    @GetMapping("/pageDef")
    fun pageDef( // @RequestParam("softName") String softName,
            @RequestParam(defaultValue = "") softName: String?,
            @RequestParam(defaultValue = "1") current: Int?,
            @RequestParam(defaultValue = "6") size: Int?
    ): String? {
        val result = softService!!.pageDef(current, size, softName)
        println("-------------------11111----------------------")
        println(result)
        log.info(result.toString())
        println("-------------------22222----------------------")
        println(result.toString())
        println("-------------------3333----------------------")
        // result.records[0] 为什么时标红的？？
        println(Gson().toJson(result))
        return Gson().toJson(result)
    }

    @PostMapping("queryFuzzy") // 错误，可以先用@GetMapping 试试 PostMapping
    fun postQueryFuzzy(@RequestBody map: HashMap<*, *>): String? {
        val current = if (map["current"] != null) map["current"] as Int? else 1
        val pageSize = if (map["pageSize"] != null) map["pageSize"] as Int? else 20
        val softId = if (map["softId"] != null) map["softId"] as String? else ""
        val softName = if (map["softName"] != null) map["softName"] as String? else ""
        val softVersion = if (map["softVersion"] != null) map["softVersion"] as String? else ""
        val gitUrl = if (map["gitUrl"] != null) map["gitUrl"] as String? else ""
        val officialUrl = if (map["officialUrl"] != null) map["officialUrl"] as String? else ""
        val mainLanguage = if (map["mainLanguage"] != null) map["mainLanguage"] as String? else ""
        val languages = if (map["languages"] != null) map["languages"] as String? else ""
        val startTime = if (map["startTime"] != null) map["startTime"] as String? else ""
        val endTime = if (map["endTime"] != null) map["endTime"] as String? else ""
        log.info("""
        current,  pageSize, softId,
        softName, softVersion, gitUrl, officialUrl,
        mainLanguage, languages, startTime, endTime::::::: {}, {}, {}, {} {} {} {} {} {} {} {}
        """.trimIndent(), current, pageSize, softId,
                    softName, softVersion, gitUrl, officialUrl,
                    mainLanguage, languages, startTime, endTime)
        val result = softService!!.queryFuzzy(current, pageSize, softId,
                softName, softVersion, gitUrl, officialUrl,
                mainLanguage, languages, startTime, endTime
        )
        return Gson().toJson(result)
        // 掉不了，有误？？
    }

}